package cn.itsource.common.service.impl;

import cn.itsource.common.mapper.BaseMapper;
import cn.itsource.common.query.BaseQuery;
import cn.itsource.common.service.BaseService;
import cn.itsource.common.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public PageList<T> findPageList(BaseQuery query) {
        //查询总条数
        Long count = baseMapper.findCountByQuery(query);
        //如果结果只为0，就证明查询总条数为0
        if(Long.compare(count,0L)==0){
            return new PageList<T>();
        }
        List<T> list = baseMapper.findAllByQuery(query);
        return new PageList<>(list,count);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> list) {
        baseMapper.batchDelete(list);
    }
}
